/* Copyright (c) 2009-2010, Sean A.O. Harney <sharney3@gmail.com> */

#include "omf.h"



//expects already evaled list
//returns list
EXPR_P builtin_func_tl(EXPR_P list)
{
    LLIST_P llist;		//llist of evaled exprs.

    if (list->type != EXPR_LEAF || list->u.leaf.type != LEAF_LIST)
	fatal_error_println
	    ("OMF built-in function tl() expects LIST as argument.");

    llist = list->u.leaf.val.list_val;

    if (llist == NULL)		//empty list. NOTE: SML tl []; throws Empty exception
	fatal_error_println
	    ("OMF built-in function tl() used on empty list [].");

    // NOTE for list of size 1,eg tl ["a"] is []. (llist->next will be NULL).
    // TODO: copy llist->next perhaps?

    LLIST_P new_llist = NULL;
    LLIST_P walker;

    for (walker = llist->next; walker != NULL; walker = walker->next)	//skip over first element, hd
	llist_append(walker->element, &new_llist);

    return new_expr_leaf_list(new_llist);
}
